package com.ilop.sthome.utils.greenDao.manage;

import android.content.Context;
import android.util.Log;

import com.example.common.utils.SpUtil;
import com.example.xmpic.ui.utils.FunSdkInitUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DaoSession;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DefaultDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.UserInfoDaoUtil;
import com.siterwell.familywellplus.BuildConfig;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * greenDao
 */
public class DaoManager {

    private static final String TAG = DaoManager.class.getSimpleName();
    private static String DB_NAME = null;
    private Context context;

    //多线程中要被共享的使用volatile关键字修饰
    private volatile static DaoManager manager = null;
    private static DBDaoMaster sDaoMaster;
    private static DBDaoMaster.OpenHelper sHelper;
    private static DaoSession sDaoSession;

    /**
     * 单例模式获得操作数据库对象
     *
     * @return
     */
    public static DaoManager getInstance() {
        if (manager == null){
            synchronized (DBDaoMaster.class){
                if (manager == null){
                    manager = new DaoManager();
                }
            }
        }
        return manager;
    }

    private DaoManager() {
        setDebug();
    }

    public void init(Context context) {
        this.context = context;
        DB_NAME = SpUtil.getString(context, CommonId.KEY_LOGIN_ID, "FamilyLink.db");
        Log.i(TAG, "init: " + DB_NAME);
        sHelper = new DBDaoMaster.OpenHelper(context, DB_NAME);
        sDaoMaster = new DBDaoMaster(sHelper.getWritableDb());
        sDaoSession = sDaoMaster.newSession();

        /* 初始化摄像头数据库 */
        FunSdkInitUtil.getInstance().setDatabase();
    }

    /**
     * 完成对数据库的添加、删除、修改、查询操作，仅仅是一个接口
     *
     * @return
     */
    public DaoSession getDaoSession() {
        return sDaoSession;
    }

    /**
     * 判断是否有存在数据库，如果没有则创建
     *
     * @return
     *//*
    public DBDaoMaster getDaoMaster() {
        if (sDaoMaster == null) {
            DB_NAME = SpUtil.getString(context, CommonId.KEY_LOGIN_ID, "FamilyLink.db");
            sHelper = new DBDaoMaster.OpenHelper(context, DB_NAME);
            sDaoMaster = new DBDaoMaster(sHelper.getWritableDb());
        }
        return sDaoMaster;
    }*/

    /**
     * 打开输出日志，默认关闭
     */
    public void setDebug() {
        if (BuildConfig.DEBUG) {
            QueryBuilder.LOG_SQL = true;
            QueryBuilder.LOG_VALUES = true;
        }
    }

    /**
     * 清空数据库
     */
    public void deleteAllSQL(){
        DBDaoMaster.dropAllTables(sDaoSession.getDatabase(),true);
        DBDaoMaster.createAllTables(sDaoSession.getDatabase(), true);
    }

    /**
     * 初始化数据库/关闭所有的操作，数据库开启后，使用完毕要关闭
     */
    public void DBInitialize() {
        manager = null;
        UserInfoDaoUtil.setInstance();
        RoomDaoUtil.setInstance();
        DeviceDaoUtil.setInstance();
        SceneDaoUtil.setInstance();
        AutomationDaoUtil.setInstance();
        DefaultDaoUtil.setInstance();
    }
}
